package com.example.hotel_lxy.ui.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.example.hotel_lxy.R;
import com.example.hotel_lxy.adapter.ReserveInfoAdapter;
import com.example.hotel_lxy.application.ARouterPath;
import com.example.hotel_lxy.bean.BeanBean;
import com.example.hotel_lxy.bean.ReserveInfoBean;
import com.example.hotel_lxy.net.Api;
import com.example.hotel_lxy.ui.dialog.DialPhoneDialog;
import com.example.hotel_lxy.ui.dialog.ImageViewpagerDialog;
import com.example.hotel_lxy.ui.dialog.SelectedInfoFoodDialog;
import com.example.hotel_lxy.ui.dialog.SelectedServiceDialog;
import com.example.hotel_lxy.ui.dialog.UnsubscribeDialog;
import com.example.hotel_lxy.utils.DatePhotoUtils;
import com.example.hotel_lxy.utils.JurisdictionUtils;
import com.example.hotel_lxy.utils.SPUtils;
import com.ysxsoft.common_base.base.BaseActivity;
import com.ysxsoft.common_base.base.RBaseAdapter;
import com.ysxsoft.common_base.base.RViewHolder;
import com.ysxsoft.common_base.utils.JsonUtils;
import com.ysxsoft.common_base.utils.ToastUtils;
import com.ysxsoft.common_base.view.custom.image.RoundImageView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * 预定信息
 */
@Route(path = "/main/ReserveInformationActivity")
public class ReserveInformationActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.back)
    TextView back;
    @Autowired
    String id;
    @BindView(R.id.text_begin_time)
    TextView textBeginTime;
    @BindView(R.id.text_b_name)
    TextView textBName;
    @BindView(R.id.text_desk)
    TextView textDesk;
    @BindView(R.id.text_d_name)
    TextView textDName;
    @BindView(R.id.text_book)
    TextView textBook;
    @BindView(R.id.text_mobile)
    TextView textMobile;

    @BindView(R.id.rv_newarrs_info)
    RecyclerView rvNewarrsInfo;
    @BindView(R.id.text_increase_money)
    TextView textIncreaseMoney;
    @BindView(R.id.text_director)
    TextView textDirector;
    @BindView(R.id.text_source)
    TextView textSource;
    @BindView(R.id.text_addtime)
    TextView textAddtime;
    @BindView(R.id.text_content)
    TextView textContent;
    @BindView(R.id.Rv_contract)
    RecyclerView RvContract;
    @BindView(R.id.Rv_other)
    RecyclerView RvOther;
    @BindView(R.id.text_detele_type)
    TextView textDeteleType;
    @BindView(R.id.text_set_type)
    TextView textSetType;
    @BindView(R.id.line_newarrs)
    LinearLayout lineNewarrs;
    @BindView(R.id.line_increase)
    LinearLayout lineIncrease;
    @BindView(R.id.rv_type)
    RecyclerView rvType;
    @BindView(R.id.line1)
    LinearLayout line1;
    @BindView(R.id.dname)
    TextView dname;
    @BindView(R.id.beizhu)
    LinearLayout beizhu;
    @BindView(R.id.textht)
    TextView textht;
    @BindView(R.id.textqt)
    TextView textqt;
    @BindView(R.id.linetu)
    LinearLayout linetu;
    private List<ContractsBean> mlist_rvhetong = new ArrayList<>();
    private List<OthersBean> mlist_rvqita = new ArrayList<>();
    private List<String> mlist_hetong = new ArrayList<>();
    private List<String> mlist_qita = new ArrayList<>();
    @Autowired
    String type;
    String begin_time;
    int is_meeting;
    public static void start(String id, String type) {
        ARouter.getInstance().build(ARouterPath.getReserveInformationActivity())
                .withString("id", id)
                .withString("type", type)
                .navigation();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_reserve_information;
    }

    @Override
    public void doWork() {
        super.doWork();
        ARouter.getInstance().inject(this);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        textDName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SelectedInfoFoodDialog.show(ReserveInformationActivity.this, null, id,is_meeting+"",textBeginTime.getText().toString());
            }
        });
        textSetType.setOnClickListener(this);
        textDeteleType.setOnClickListener(this);
        lineIncrease.setOnClickListener(this);
        textMobile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (DatePhotoUtils.isPhone(textMobile.getText().toString() + "")) {
                    DialPhoneDialog.show(ReserveInformationActivity.this,null,textMobile.getText().toString()+"");
                } else {
                    com.luck.picture.lib.tools.ToastUtils.s(ReserveInformationActivity.this, "手机号有误");
                }
//
//
//                Intent intent = new Intent(Intent.ACTION_DIAL);
//                Uri data = Uri.parse("tel:" + textMobile.getText().toString());
//                intent.setData(data);
//                startActivity(intent);
            }
        });
        showDataInfo();
    }


    private void showDataInfo() {
        showLoadingDialog();
        OkHttpUtils.post()
                .url(Api.POST_SCHEDULED_ORDER_dEL)
                .addHeader("flag", SPUtils.get(ReserveInformationActivity.this, "token", "").toString())
                .addParams("id", id + "")
                .addParams("is_del", "" + type)
                .tag(this)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("TAG", "预定信息onError   " + e.getMessage());
                        hideLoadingDialog();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        hideLoadingDialog();
                        Log.e("TAG", "预定信息onResponse   " + response);
                        BeanBean beanBean = JsonUtils.parseByGson(response, BeanBean.class);
                        if (beanBean.getCode().equals("4")) {
                            SPUtils.put(mContext, "token", "");
                            Intent intent = new Intent(mContext, LoginActivity.class).
                                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                            return;
                        }
                        if (beanBean.getCode().equals("0")) {
                            ReserveInfoBean reserveInfoBean = JsonUtils.parseByGson(response, ReserveInfoBean.class);
                            ReserveInfoBean.DataBeanX.DataBean order = reserveInfoBean.getData().getData();
                            if (order.getIs_meeting() == 1) {
                                 is_meeting = order.getIs_meeting();
                                String enddata;
                                if (order.getEnd_night().equals("1")) {
                                    enddata = order.getEnd_time() + "上午";
                                } else {
                                    enddata = order.getEnd_time() + "下午";
                                }
                                begin_time = order.getBegin_time();
                                if (order.getBegin_night() == 2) {
                                    textBeginTime.setText("【宴会时间】" + order.getBegin_time() + "下午-" + enddata);
                                } else {
                                    textBeginTime.setText("【宴会时间】" + order.getBegin_time() + "上午-" + enddata);
                                }
                                textDesk.setText("【人 数】" + order.getDesk() + "人");
                            } else {
                                begin_time = order.getBegin_time();
                                if (order.getBegin_night() == 2) {
                                    textBeginTime.setText("【宴会时间】" + order.getBegin_time() + "晚上");
                                } else {
                                    textBeginTime.setText("【宴会时间】" + order.getBegin_time() + "中午");
                                }
                                textDesk.setText("【桌数】" + order.getDesk() + "桌 备" + order.getPrepare_desk() + "桌");
                            }


                            textBName.setText("【宴会厅】" + order.getB_name() + "         " + order.getT_name());


                            if (order.getD_name() != null) {
                                if(!order.getD_name().trim().equals("")){
                                    dname.setVisibility(View.VISIBLE);
                                    textDName.setText("" + order.getD_name());
                                }else{
                                    textDName.setText("");
                                    dname.setVisibility(View.GONE);
                                }
                            } else {
                                textDName.setText("");
                                dname.setVisibility(View.GONE);
                            }
                            if(order.getSex()==1){
                                textBook.setText("" + order.getBook()+" 先生");
                            }else if(order.getSex()==3){
                                textBook.setText("" + order.getBook()+" 女士");
                            }else{
                                textBook.setText("" + order.getBook());
                            }

                            textMobile.setText("" + order.getMobile());

                            rvType.setLayoutManager(new LinearLayoutManager(ReserveInformationActivity.this));
                            RBaseAdapter<ReserveInfoBean.DataBeanX.NewArrBean> valueBeanRBaseAdapter = new RBaseAdapter<ReserveInfoBean.DataBeanX.NewArrBean>
                                    (ReserveInformationActivity.this,
                                            R.layout.item_reserve_info_rv_type, reserveInfoBean.getData().getNewArr()) {
                                @Override
                                protected void fillItem(RViewHolder holder, ReserveInfoBean.DataBeanX.NewArrBean item, int position) {
                                    TextView name = holder.itemView.findViewById(R.id.name);
                                    TextView names = holder.itemView.findViewById(R.id.names);
                                    TextView groomname = holder.itemView.findViewById(R.id.groomname);
                                    TextView groomphoto = holder.itemView.findViewById(R.id.groomphoto);
                                    name.setText("" + item.getTitle());
                                    names.setText("" + item.getMobile());
                                    if (item.getNames() != null) {
                                        groomname.setText("" + item.getNames());
                                    }
                                    if (item.getMobiles() != null) {
                                        groomphoto.setText("" + item.getMobiles());
                                    }
                                    groomphoto.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            if (DatePhotoUtils.isPhone(item.getMobiles() + "")) {
                                                DialPhoneDialog.show(ReserveInformationActivity.this,null,item.getMobiles()+"");
                                            } else {
                                                com.luck.picture.lib.tools.ToastUtils.s(ReserveInformationActivity.this, "手机号有误");
                                            }

//                                            Intent intent = new Intent(Intent.ACTION_DIAL);
//                                            Uri data = Uri.parse("tel:" + item.getMobiles());
//                                            intent.setData(data);
//                                            startActivity(intent);
                                        }
                                    });
                                }

                                @Override
                                protected int getViewType(ReserveInfoBean.DataBeanX.NewArrBean item, int position) {
                                    return 0;
                                }
                            };

                            rvType.setAdapter(valueBeanRBaseAdapter);


                            List<ReserveInfoBean.DataBeanX.ScheduledDepositBean> newArrs = reserveInfoBean.getData().getScheduled_deposit();
                            if (newArrs.size() == 0) {
                                lineNewarrs.setVisibility(View.GONE);
                            }
                            rvNewarrsInfo.setLayoutManager(new LinearLayoutManager(ReserveInformationActivity.this));
                            ReserveInfoAdapter reserveInfoAdapter = new ReserveInfoAdapter(ReserveInformationActivity.this, newArrs);
                            rvNewarrsInfo.setAdapter(reserveInfoAdapter);
                            if (order.getIncrease_money() != null) {
                                if(!order.getIncrease_money().equals("0.00")){
                                    textIncreaseMoney.setText("" + order.getIncrease_money()+"元");
                                }else{
                                    lineIncrease.setVisibility(View.GONE);
                                }
                            } else {
                                lineIncrease.setVisibility(View.GONE);
                            }


                            textDirector.setText("【客户经理】" + order.getDirector());
                            textSource.setText("【客户来源】" + order.getSource());
                            textAddtime.setText("【预订时间】" + order.getAddtime());
                            if (order.getContent() != null) {
                                if(!order.getContent().trim().equals("")){
                                    textContent.setText("" + order.getContent());
                                }else{
                                    beizhu.setVisibility(View.GONE);
                                }
                            }else{
                                beizhu.setVisibility(View.GONE);
                            }

                            //合同
                            if (reserveInfoBean.getData().getData().getContracts() != null) {
                                mlist_hetong.clear();
                                mlist_rvhetong.clear();
                                for (int i = 0; i < reserveInfoBean.getData().getData().getContracts().size(); i++) {
                                    ReserveInfoBean.DataBeanX.DataBean.ContractsBean contractsBean = reserveInfoBean.getData().getData().getContracts().get(i);
                                    ContractsBean contractsBea = new ContractsBean();
                                    contractsBea.setContract_id(contractsBean.getContract_id());
                                    mlist_hetong.add(contractsBean.getContracts());
                                    contractsBea.setContracts(contractsBean.getContracts());
                                    mlist_rvhetong.add(contractsBea);
                                }

                                RvContract.setLayoutManager(new GridLayoutManager(ReserveInformationActivity.this, 3));

                                RBaseAdapter<String> rBaseAdapter = new RBaseAdapter<String>(
                                        ReserveInformationActivity.this, R.layout.item_img, mlist_hetong) {
                                    @Override
                                    protected void fillItem(RViewHolder holder, String item, int position) {
                                        ImageView img = holder.itemView.findViewById(R.id.img);
                                        Glide.with(ReserveInformationActivity.this).load(item).into(img);
                                        holder.itemView.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                ImageViewpagerDialog.show(ReserveInformationActivity.this, null, mlist_hetong, position);
                                            }
                                        });

                                    }

                                    @Override
                                    protected int getViewType(String item, int position) {
                                        return 0;
                                    }
                                };
                                RvContract.setAdapter(rBaseAdapter);
                            }else{
                                textht.setVisibility(View.GONE);
                            }
                            //其他
                            if (reserveInfoBean.getData().getData().getOthers() != null) {
                                mlist_rvqita.clear();
                                mlist_qita.clear();
                                for (int i = 0; i < reserveInfoBean.getData().getData().getOthers().size(); i++) {
                                    ReserveInfoBean.DataBeanX.DataBean.OthersBean othersBean1 = reserveInfoBean.getData().getData().getOthers().get(i);
                                    OthersBean othersBean = new OthersBean();
                                    othersBean.setOther_id(othersBean1.getOther_id());
                                    othersBean.setOthers(othersBean1.getOthers());
                                    mlist_rvqita.add(othersBean);
                                    mlist_qita.add(othersBean1.getOthers());
                                }
                                RvOther.setLayoutManager(new GridLayoutManager(ReserveInformationActivity.this, 3));
                                RBaseAdapter<String> rBaseAdapterother = new RBaseAdapter<String>(
                                        ReserveInformationActivity.this, R.layout.item_img, mlist_qita) {
                                    @Override
                                    protected void fillItem(RViewHolder holder, String item, int position) {
                                        RoundImageView img = holder.itemView.findViewById(R.id.img);
                                        Glide.with(ReserveInformationActivity.this).load(item).into(img);
                                        holder.itemView.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                ImageViewpagerDialog.show(ReserveInformationActivity.this, null, mlist_qita, position);
                                            }
                                        });

                                    }

                                    @Override
                                    protected int getViewType(String item, int position) {
                                        return 0;
                                    }
                                };
                                RvOther.setAdapter(rBaseAdapterother);
                            }else{
                                textqt.setVisibility(View.GONE);
                            }
                            if(reserveInfoBean.getData().getData().getOthers() == null&&reserveInfoBean.getData().getData().getContracts()==null){
                                linetu.setVisibility(View.GONE);
                            }

                            ReserveInfoBean.DataBeanX.RoleBean role = reserveInfoBean.getData().getRole();
                            if (role.getOrder_type() == 1) {
                                textDeteleType.setVisibility(View.GONE);
                                textSetType.setText("关闭");
                            } else if (role.getOrder_type() == 2) {
                                textSetType.setText("修改信息");
                                textDeteleType.setVisibility(View.GONE);
                            } else if (role.getOrder_type() == 3) {
                                if (reserveInfoBean.getData().getScheduled_deposit() == null || reserveInfoBean.getData().getScheduled_deposit().size() == 0) {
                                    textSetType.setVisibility(View.GONE);
                                    textDeteleType.setText("取消潜客");
                                } else {
                                    textSetType.setVisibility(View.GONE);
                                    textDeteleType.setText("取消预订");
                                }
                            } else if (role.getOrder_type() == 4) {
                                textDeteleType.setVisibility(View.GONE);
                                textSetType.setText("订单结算");
                            } else if (role.getOrder_type() == 5) {
                                if (reserveInfoBean.getData().getScheduled_deposit() == null || reserveInfoBean.getData().getScheduled_deposit().size() == 0) {
                                    textSetType.setText("转为预订");
                                    textDeteleType.setText("取消潜客");
                                } else {
                                    textSetType.setText("修改信息");
                                    textDeteleType.setText("取消预订");
                                }
                            } else if (role.getOrder_type() == 6) {
                                textDeteleType.setText("修改信息");
                                textSetType.setText("订单结算");
                            }
                        } else {
                            ToastUtils.shortToast(ReserveInformationActivity.this, beanBean.getMsg() + "");
                            textDeteleType.setEnabled(false);
                            textSetType.setEnabled(false);
                            finish();
                        }

                    }
                });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.text_set_type:
                if (textSetType.getText().toString().equals("订单结算")) {
                    StatementActivity.start(id);
                    finish();
                } else if (textSetType.getText().toString().equals("修改信息")) {
                    SetUpReserveActivity.start(id);
                    finish();
                } else if (textSetType.getText().toString().equals("转为预订")) {
                    SetUpReserveActivity.start(id);
                    finish();
                } else if (textSetType.getText().toString().equals("关闭")) {
                    finish();
                }
                break;
            case R.id.text_detele_type:
                if (textDeteleType.getText().toString().equals("取消预订") || textDeteleType.getText().toString().equals("取消潜客")) {
                    UnsubscribeDialog.show(mContext,null).setListener(new UnsubscribeDialog.OnDialogClickListener() {
                        @Override
                        public void onPositiveClick(String s) {
                            if(!s.equals("")){
                                showDetele();
                            }
                        }
                    });
                } else if (textDeteleType.getText().toString().equals("修改信息") || textDeteleType.getText().toString().equals("转为预订")) {
                    SetUpReserveActivity.start(id);
                }
                break;
            case R.id.line_increase:
                SelectedServiceDialog.show(ReserveInformationActivity.this, null, id,is_meeting+"",textBeginTime.getText().toString());
                break;
        }
    }

    private void showDetele() {
        OkHttpUtils.get()
                .url(Api.POST_LATENT_DELETE)
                .addHeader("flag", SPUtils.get(mContext, "token", "").toString())
                .addParams("id", "" + id)
                .tag(this)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("TAG", "潜客预留信息Exception   " + e.getMessage());

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("TAG", "潜客预留信息onResponse   " + response);
                        BeanBean beanBean = JsonUtils.parseByGson(response, BeanBean.class);
                        if (beanBean.getCode().equals("0")) {
                            SPUtils.put(ReserveInformationActivity.this, "aBoolean", "1");
                            SPUtils.put(ReserveInformationActivity.this, "currentDate", begin_time+"");
                            finish();
                        } else if (beanBean.getCode().equals("4")) {
                            SPUtils.put(mContext, "token", "");
                            Intent intent = new Intent(mContext, LoginActivity.class).
                                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                            return;
                        }

                    }
                });
    }

    public class OthersBean {
        /**
         * other_id : 91
         * others : http://lxy.yskxfs.com/uploads/uploads/apifile/20210423/09efaaf046905a94cb29fb342827eca7.png
         */

        private String other_id;
        private String others;

        public String getOther_id() {
            return other_id;
        }

        public void setOther_id(String other_id) {
            this.other_id = other_id;
        }

        public String getOthers() {
            return others;
        }

        public void setOthers(String others) {
            this.others = others;
        }
    }

    public class ContractsBean {
        /**
         * contract_id : 92
         * contracts : http://lxy.yskxfs.com/uploads/uploads/apifile/20210423/75d4d96f391a9497b768cbdd2683f42f.jpg
         */

        private String contract_id;
        private String contracts;

        public String getContract_id() {
            return contract_id;
        }

        public void setContract_id(String contract_id) {
            this.contract_id = contract_id;
        }

        public String getContracts() {
            return contracts;
        }

        public void setContracts(String contracts) {
            this.contracts = contracts;
        }
    }
}